package com.rede.didiok.user.service.impl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.user.FeedbackDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.holder.RequestHolder;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.user.dao.FeedbackDao;
import com.rede.didiok.user.entity.FeedbackEntity;
import com.rede.didiok.user.entity.UserEntity;
import com.rede.didiok.user.service.FeedbackService;
import com.rede.didiok.user.service.UserService;


@Service("feedbackService")
public class FeedbackServiceImpl extends ServiceImpl<FeedbackDao, FeedbackEntity> implements FeedbackService {

    @Autowired
    private UserService userService;

    /***************************** didiok-admin:start **************************/
    // region  didiok-admin

    /**
     * 获取反馈列表
     *
     * @param feedbackDto
     * @return
     */
    @Override
    public IPage<FeedbackEntity> getPageList(FeedbackDto feedbackDto) {
        QueryWrapper<FeedbackEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(feedbackDto.getTitle())) {
            queryWrapper.like(SQLConf.TITLE, feedbackDto.getTitle());
        }

        if (feedbackDto.getFeedbackStatus() != null) {
            queryWrapper.eq(SQLConf.FEEDBACK_STATUS, feedbackDto.getFeedbackStatus());
        }

        Page<FeedbackEntity> page = new Page<>();
        page.setCurrent(feedbackDto.getCurrentPage());
        page.setSize(feedbackDto.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<FeedbackEntity> pageList = this.page(page, queryWrapper);

        List<FeedbackEntity> feedbackList = pageList.getRecords();
        List<String> userUids = new ArrayList<>();
        feedbackList.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getCreateUserUid())) {
                userUids.add(item.getCreateUserUid());
            }
        });
        List<UserEntity> userList = userService.getUserListByIds(userUids);
        Map<String, UserEntity> map = new HashMap<>();
        userList.forEach(item -> {
            item.setPassword("");
            map.put(item.getUid(), item);
        });

        feedbackList.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getCreateUserUid())) {
                item.setUser(map.get(item.getCreateUserUid()));
            }
        });

        pageList.setRecords(feedbackList);
        return pageList;
    }

    /**
     * 新增反馈
     *
     * @param feedbackDto
     */
    @Override
    public String addFeedback(FeedbackDto feedbackDto) {
        HttpServletRequest request = RequestHolder.getRequest();
        if (request.getAttribute(SysConf.ADMIN_UID) != null) {
            ResultUtil.errorWithMessage(MessageConf.OPERATION_FAIL);
        }
        FeedbackEntity feedback = this.getById(feedbackDto.getUid());
        feedback.setTitle(feedbackDto.getTitle());
        feedback.setContent(feedbackDto.getContent());
        feedback.setFeedbackStatus(feedbackDto.getFeedbackStatus());
        feedback.setReply(feedbackDto.getReply());
        if (request.getAttribute(SysConf.ADMIN_UID) != null) {
            feedback.setAdminUid(request.getAttribute(SysConf.ADMIN_UID).toString());
        }
        feedback.updateById();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 批量删除反馈
     *
     * @param feedbackDtoList
     */
    @Override
    public String deleteBatchFeedback(List<FeedbackDto> feedbackDtoList) {
        HttpServletRequest request = RequestHolder.getRequest();
        if (request.getAttribute(SysConf.ADMIN_UID) != null) {
            ResultUtil.errorWithMessage(MessageConf.OPERATION_FAIL);
        }
        final String adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
        if (feedbackDtoList.size() <= 0) {
            return ResultUtil.errorWithMessage(MessageConf.PARAM_INCORRECT);
        }
        List<String> uids = new ArrayList<>();
        feedbackDtoList.forEach(item -> {
            uids.add(item.getUid());
        });

        Collection<FeedbackEntity> feedbackList = this.listByIds(uids);

        feedbackList.forEach(item -> {
            item.setAdminUid(adminUid);
            item.setUpdateTime(new Date());
            item.setStatus(StatusEnum.DISABLED);
        });

        Boolean save = this.updateBatchById(feedbackList);

        if (save) {
            return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);
        } else {
            return ResultUtil.errorWithMessage(MessageConf.DELETE_FAIL);
        }
    }

    // endregion
    /***************************** didiok-admin:end **************************/



    /***************************** didiok-web:start **************************/
    // region didiok-web

    // endregion
    /***************************** didiok-web:end **************************/

}